package com.tony.thread;

import java.util.Date;
import java.util.concurrent.Semaphore;

public class SemaphoreTest {
    static class Test implements  Runnable{
        private Semaphore semaphore;
        public Test(Semaphore semaphore){
            this.semaphore = semaphore;
        }
        @Override
        public void run() {
            try {
                semaphore.acquire(3);
                System.out.println(Thread.currentThread().getName()+":"+ new Date().getTime());
                Thread.sleep(1000);
                System.out.println("..........."+semaphore.availablePermits());
                semaphore.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(10);
        Test t = new Test(semaphore);
        for(int i=0;i<15;i++){
            new Thread(t).start();
        }
    }

}
